package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponMapper;
import com.qf.pojo.DtsCoupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author sunyamei
 * @version V1.0
 * @Project javase2203
 * @Package com.qf.service
 * @Description:
 * @Date 2022/7/30 16:00
 */
@Service
public class CouponServiceImpl  implements  CouponService{

    @Autowired
    private DtsCouponMapper couponMapper;

    //服务器地址
    @Value("${aliyun.oss.endpoint}")
    private  String endpoint;

    //子账号
    @Value("${aliyun.oss.accessKeyId}")
    private  String accessKeyId;

    //子账号密码
    @Value("${aliyun.oss.accessKeySecret}")
    private  String accessKeySecret;

    //桶的名字
    @Value("${aliyun.oss.bucketName}")
    private  String bucketName;



    /**
     * 分页查询
     * @param name
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @return
     */
    @Override
    public IPage<DtsCoupon> queryByPage(String name, Integer page, Integer limit, String sort, String order) {

        //创建分页对象
        IPage<DtsCoupon> ipage = new Page<>(page,limit);

        //创建查询对象
        QueryWrapper<DtsCoupon> queryWrapper = new QueryWrapper<>();

        //判断是否为空
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name", name);
        }


        //排序
        if (!StringUtils.isEmpty(sort) &&  !StringUtils.isEmpty(order)){
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }

      ipage = couponMapper.selectPage(ipage, queryWrapper);

        return ipage;
    }

    @Override
    public DtsCoupon add(DtsCoupon coupon) {
        int insert = couponMapper.insert(coupon);
        return coupon;
    }


}
